
package com.itmayiedu.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {


	@Value("${server.port}")
	private String port;

	/****
	 * 修改端口，启动两次，可以访问不同的端口来实现nginx的负载均衡的测试 可以显示不同的端口号
	 * @return
	 */
	@RequestMapping("/index")
	public String index() {
		return "server---value:" + port;
	}
	
	/****
	 * 测试nginx的session共享的方法   通过设置key和lue
	 * 然后在getsession的方法中来得到value的值 来测试是否是共享session
	 * @param request
	 * @param key
	 * @param value
	 * @return
	 */
	@RequestMapping("/setSession")
	public String setSession(HttpServletRequest request, String key, String value) {
		HttpSession session = request.getSession();
		session.setAttribute(key, value);
		return "server---port:" + port + ",success";
	}

	@RequestMapping("/getSession")
	public String getSession(HttpServletRequest request, String key) {
		HttpSession session = null;
		try {
			session = request.getSession(false);
		} catch (Exception e) {
			// TODO: handle exception
		}
		String value = null;
		if (session != null) {
			value = (String) session.getAttribute(key);
		}

		return "server---port:" + port + "  value:" + value;
	}
	
}
